package com.gxuwz.prrs.activity;

import android.os.Bundle;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import com.gxuwz.prrs.R;
import com.gxuwz.prrs.bean.LoginForm;
import com.gxuwz.prrs.service.impl.AuthServiceImpl;
import com.gxuwz.prrs.service.ServiceHolder;
import com.gxuwz.prrs.utils.NavigationUtils;

/**
 * @author 张钧
 * 登录页面
 */
public class LoginActivity extends AppCompatActivity {

	private final ServiceHolder serviceholder = ServiceHolder.getInstance();

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_login);
		TextView studentNumber = findViewById(R.id.studentNumber);
		TextView password = findViewById(R.id.password);
		Button loginButton = findViewById(R.id.login_button);
		loginButton.setOnClickListener(view -> {
			AuthServiceImpl authService = serviceholder.getBean(AuthServiceImpl.class);
			LoginForm loginForm = new LoginForm();
			loginForm.setPassword(password.getText().toString());
			loginForm.setStudentNumber(studentNumber.getText().toString());
			boolean login = authService.login(loginForm);
			if(login){
				Toast.makeText(this, "登陆成功", Toast.LENGTH_SHORT).show();
				NavigationUtils.redirectToMainActivity(this,null);
			}else {
				Toast.makeText(this, "学号或密码错误", Toast.LENGTH_SHORT).show();
			}
		});
	}
}
